package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.entity.enums.eTradeWarBuildingType;
import com.yanqu.road.entity.log.LogTradeWarBuildingUpgrade;
import com.yanqu.road.entity.tradewar.TradeWarBuilding;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.tradewar.UserTradeWarBuilding;
import com.yanqu.road.pb.tradewar.TradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.TradeWarBuildingModule;
import com.yanqu.road.server.manger.config.TradeWarConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


@Cmd(code = Protocol.S_TRADE_WAR_BUILDING_UPGRADE, desc = "商战建筑升级")
public class BuildingUpgradeCmd extends AbstractCommand {

	private int type;

	private Property reward;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TradeWarProto.TradeWarBuildingUpgradeReqMsg reqMsg = TradeWarProto.TradeWarBuildingUpgradeReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		TradeWarBuildingModule tradeWarBuildingModule = player.getModule(TradeWarBuildingModule.class);
		UserTradeWarBuilding taxOffice = tradeWarBuildingModule.getUserTradeWarBuilding(eTradeWarBuildingType.TaxOffice.getValue());
		if(null == taxOffice){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		UserTradeWarBuilding userTradeWarBuilding = tradeWarBuildingModule.getUserTradeWarBuilding(type);
		if(null == userTradeWarBuilding){
			ret = GameErrorCode.E_TRADE_WAR_BUILDING_IS_LOCK;
			return;
		}
		//判断是否到最高级别
		TradeWarBuilding nextTradeWarBuilding = TradeWarConfigMgr.getTradeWarBuilding(type, userTradeWarBuilding.getLevel() + 1);
		if(null == nextTradeWarBuilding){
			ret = GameErrorCode.E_TRADE_WAR_BUILDING_LEVEL_MAX;
			return;
		}
		TradeWarBuilding tradeWarBuilding = TradeWarConfigMgr.getTradeWarBuilding(type, userTradeWarBuilding.getLevel());
		if(taxOffice.getLevel() < tradeWarBuilding.getUpgradeLimit()){
			ret = GameErrorCode.E_TRADE_WAR_TAX_OFFICE_LEVEL_NO_ENOUGH;
			return;
		}
		//消耗
		if(!player.getModule(CurrencyModule.class).removeCurrency(tradeWarBuilding.getUpgradeConsume(), eLogMoneyType.TradeWar,
				eLogMoneyType.TradeWarBuildingUpgrade)){
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}
		reward = tradeWarBuildingModule.buildingUpgrade(type);
		AutoLogMgr.add(new LogTradeWarBuildingUpgrade(player.getUserId(), type, userTradeWarBuilding.getLevel(), PropertyHelper.parsePropertyToString(tradeWarBuilding.getUpgradeConsume())));
	}

	@Override
	public void sendData(GamePlayer player) {
		TradeWarProto.TradeWarBuildingUpgradeRespMsg.Builder respMsg = TradeWarProto.TradeWarBuildingUpgradeRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
		}
		player.sendPacket(Protocol.U_TRADE_WAR_BUILDING_UPGRADE, respMsg);
	}
}
